home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 039a / tc_tsr10.zip / PRT2DISK.C < prev    next >
Text File  |  1991-06-18  |  6KB  |  181 lines

  1. /*--------------------------------------------------------------------------*
  2.  |   Prt2Disk                                                               |
  3.  |     redirects printer (lpt1) output to a disk file.                      |
  4.  |   Prt2Disk is supplied as is with no warranty, expressed or implied.     |
  5.  |   It is not copyrighted, either.                                         |
  6.  |   Usage: Prt2Disk [filename]                                             |
  7.  |   Compile with: TCC -mt Prt2Disk.C                                       |
  8.  *--------------------------------------------------------------------------*/
  9.  
  10. /*--------------------------------------------------------------------------*
  11.  | Author:                                                                  |
  12.  | Sherif El-Kassas        .       :.                                       |
  13.  | EB dept           \_____o__/ __________                                  |
  14.  | Eindhoven U of Tec       .. /                                            |
  15.  | The Netherlands            /             Email: elkassas@eb.ele.tue.nl   |
  16.  *--------------------------------------------------------------------------*/
  17.  
  18. /*--------------------------------------------------------------------------*
  19.  | The author shall not be liable to the user for any direct, indirect      |
  20.  | or consequential loss arising from the use of, or inability to use,      |
  21.  | any program or file howsoever caused.  No warranty is given that the     |
  22.  | programs will work under all circumstances.                              |
  23.  *--------------------------------------------------------------------------*/
  24.  
  25. #include <string.h>
  26. #include <dos.h>
  27. #include <fcntl.h>
  28. #include <sys\stat.h>
  29. #include <io.h>
  30.  
  31. #define TRUE           (1)
  32. #define FALSE          (0)
  33. #define MAX_TABLE_SIZE (8*1024)
  34. #define MIN_BLOCK_SIZE (1024)
  35. #define cli()          __cli__()
  36. #define sti()          __sti__()
  37.  
  38. typedef unsigned char byte;
  39.  
  40. void           do_main_task(void);
  41. extern void    main_task(void);
  42. extern void    _restorezero(void);
  43.  
  44. void interrupt (* old_intr_0x17)(void);
  45. void interrupt (* old_intr_0x28)(void);
  46.  
  47. unsigned _heaplen = 1;                   /* set minimum heap size         */
  48.  
  49. unsigned c_ss, c_sp;        /* c_ss, c_sp are used to save PRTSC'S stack  */
  50. unsigned save_ss, save_sp;  /* save_ss and save_sp are used to save the   */
  51.                             /* active stack                               */
  52.  
  53. byte active = FALSE;  /* program active flag (to prevent recursion)       */
  54. byte wanted = FALSE;  /* wanted is set to TRUE if the Hot key was pressed */
  55.                       /* but the program could not be activated           */
  56.  
  57. byte far * dos_active;                        /* pointer to DOS busy flag */
  58.  
  59. byte print2disk = TRUE;
  60.  
  61. char file_name[128]="c:\\output.p2d";
  62.  
  63. char table[MAX_TABLE_SIZE];
  64. int  count=0;
  65.  
  66. void interrupt intr_0x17( unsigned bp, unsigned di, unsigned si,
  67.                           unsigned ds, unsigned es, unsigned dx,
  68.                           unsigned cx, unsigned bx, unsigned ax )
  69. {
  70.   sti();
  71.   if ( print2disk ){
  72.     ++active;
  73.     /*cli();*/
  74.     if (active == 1){
  75.       if (count > (MAX_TABLE_SIZE >> 1))
  76.         if (! *dos_active){
  77.           wanted = FALSE;
  78.           /*sti();*/
  79.           do_main_task();
  80.           /*cli();*/
  81.         }
  82.         else wanted =  TRUE;
  83.     }
  84.  
  85.     /*sti();*/
  86.     while( active != 1 ) ;
  87.  
  88.     switch ( ax >> 8){
  89.       case 0:    table[count++] = (char) ax; break;
  90.       case 1:    ax=0x9000; break;
  91.       case 2:    ax=0x9000+(char)ax; break;
  92.       case 0xFD: print2disk = TRUE; break;
  93.       case 0xFE: print2disk = FALSE;
  94.       case 0xFF: wanted = (count>0); break;
  95.     }
  96.  
  97.     --active;
  98.   }/*if (print2disk */
  99.   else {
  100.     print2disk = ((ax >> 8) == 0xFD);
  101.     _DX = dx; _BX = bx; _CX = cx; _AX = ax;
  102.     (* old_intr_0x17)();
  103.   }
  104. }
  105.  
  106. void main_task(void)
  107. {
  108. int hand = open(file_name, O_WRONLY|O_APPEND|O_CREAT, S_IREAD|S_IWRITE);
  109.   if (hand > -1){
  110.     _write(hand, table, count);
  111.     count=0;
  112.     close(hand);
  113.   }
  114. }
  115.  
  116. /*------------------------------------------------------------------------*
  117.  |                       Interrupt 0x28 handler                           |
  118.  *------------------------------------------------------------------------*/
  119. void interrupt intr_0x28(){
  120.   (* old_intr_0x28)();
  121.   sti();
  122.   if (wanted || (count >= MIN_BLOCK_SIZE)){
  123.     ++active;
  124.     /*cli();*/
  125.     if ( active == 1){
  126.       /*sti();*/
  127.       wanted = FALSE;
  128.       do_main_task();
  129.       /*cli();*/
  130.     }
  131.     --active;
  132.   }
  133. } /* intr_0x28 */
  134.  
  135.  
  136. void do_main_task(void)
  137. {
  138.   cli();
  139.   save_ss = _SS; save_sp = _SP;
  140.   _SS = c_ss; _SP = c_sp;
  141.   sti();
  142.   main_task();
  143.   cli();
  144.   _SS = save_ss; _SP = save_sp;
  145.   sti();
  146. }
  147.  
  148.  
  149. byte far * get_dos_flag(void){   /* get a pointer to DOS busy flag        */
  150.      union  REGS  reg;
  151.      struct SREGS s_reg;
  152.      reg.x.ax = 0x3400;                /* function 0x34 get DOS busy flag */
  153.      intdosx(®, ®, &s_reg);      /* call DOS (interrupt 0x21)       */
  154.      return( MK_FP(s_reg.es, reg.x.bx) ); /* return far pointer ES:BX     */
  155.  
  156. }/* get_dos_flag() */
  157.  
  158.  
  159. unsigned program_size(void){
  160.     return(* ((unsigned far *) (MK_FP(_psp-1, 3))) );
  161. }
  162.  
  163. main(int ac, char **av)
  164. {
  165.   if (ac > 1)
  166.     strcpy( file_name, av[1] );
  167.  
  168.   c_ss = _SS; c_sp = _SP;
  169.   dos_active = get_dos_flag();
  170.  
  171.   _restorezero();
  172.  
  173.   old_intr_0x28 = getvect(0x28);
  174.   setvect( 0x28, intr_0x28 );
  175.  
  176.   old_intr_0x17 = getvect(0x17);
  177.   setvect( 0x17, intr_0x17 );
  178.  
  179.   keep(0, program_size());
  180. }
  181.